<?php
class HTML
{
 // *******************************************'
	// COPY FROM class.tslib_content.php: / BEGIN
	// substituteSubpart
	// Cleaned locally 2/2003 !!!! (so different from tslib_content version)
	// *******************************************'

	/**
	 * Returns the first subpart encapsulated in the marker, $marker (possibly present in $content as a HTML comment)
	 *
	 * @param	string		Content with subpart wrapped in fx. "###CONTENT_PART###" inside.
	 * @param	string		Marker string, eg. "###CONTENT_PART###"
	 * @return	string
	 */
	function getSubpart($content, $marker)	{
		$start = strpos($content, $marker);
		if ($start===false)	{ return ''; }
		$start += strlen($marker);
		$stop = strpos($content, $marker, $start);
			// Q: What shall get returned if no stop marker is given /*everything till the end*/ or nothing
		if ($stop===false)	{ return /*substr($content, $start)*/ ''; }
		$content = substr($content, $start, $stop-$start);
		$matches = array();
		if (preg_match('/^([^\<]*\-\-\>)(.*)(\<\!\-\-[^\>]*)$/s', $content, $matches)===1)	{
			return $matches[2];
		}
		$matches = array();
		if (preg_match('/(.*)(\<\!\-\-[^\>]*)$/s', $content, $matches)===1)	{
			return $matches[1];
		}
		$matches = array();
		if (preg_match('/^([^\<]*\-\-\>)(.*)$/s', $content, $matches)===1)	{
			return $matches[2];
		}
		return $content;
	}
	function getSubparts($content, $marker)	{
		$i=0;
//		$substrCount=substr_count($content,$marker)/2;
//$text2 = 'gcdgcdgcd';
//echo substr_count($content, $marker);
		$subpartsArray=Array();
		
		while($start = strpos($content, $marker))
		{
			$subpartsArray[]=HTML::getSubpart($content,$marker);
			$end = strpos($content, $marker,$start+strlen($marker));
			$content=substr($content,$end+strlen($marker));
			$i++;
		}
		return $subpartsArray;
	}
	
	function detectSubpart($content, $marker)	{
		$start = strpos($content, $marker);
		return $start!==false;
	}
	function detectMarker($content, $marker)	{
		$start = strpos($content, $marker);
		return $start!==false;
	}
	/**
	 * Substitutes a subpart in $content with the content of $subpartContent.
	 *
	 * @param	string		Content with subpart wrapped in fx. "###CONTENT_PART###" inside.
	 * @param	string		Marker string, eg. "###CONTENT_PART###"
	 * @param	array		If $subpartContent happens to be an array, it's [0] and [1] elements are wrapped around the content of the subpart (fetched by getSubpart())
	 * @param	boolean		If $recursive is set, the function calls itself with the content set to the remaining part of the content after the second marker. This means that proceding subparts are ALSO substituted!
	 * @param	boolean		If set, the marker around the subpart is not removed, but kept in the output
	 * @return	string		Processed input content
	 */
	function substituteSubpart($content,$marker,$subpartContent,$recursive=1,$keepMarker=0)	{
		$start = strpos($content, $marker);
		if ($start===false)	{ return $content; }
		$startAM = $start+strlen($marker);
		$stop = strpos($content, $marker, $startAM);
		if ($stop===false)	{ return $content; }
		$stopAM = $stop+strlen($marker);
		$before = substr($content, 0, $start);
		$after = substr($content, $stopAM);
		$between = substr($content, $startAM, $stop-$startAM);

		if ($recursive)	{
			$after = HTML::substituteSubpart($after, $marker, $subpartContent, $recursive, $keepMarker);
		}

		if ($keepMarker)	{
			$matches = array();
			if (preg_match('/^([^\<]*\-\-\>)(.*)(\<\!\-\-[^\>]*)$/s', $between, $matches)===1)	{
				$before .= $marker.$matches[1];
				$between = $matches[2];
				$after = $matches[3].$marker.$after;
			} elseif (preg_match('/^(.*)(\<\!\-\-[^\>]*)$/s', $between, $matches)===1)	{
				$before .= $marker;
				$between = $matches[1];
				$after = $matches[2].$marker.$after;
			} elseif (preg_match('/^([^\<]*\-\-\>)(.*)$/s', $between, $matches)===1)	{
				$before .= $marker.$matches[1];
				$between = $matches[2];
				$after = $marker.$after;
			} else	{
				$before .= $marker;
				$after = $marker.$after;
			}
		} else	{
			$matches = array();
			if (preg_match('/^(.*)\<\!\-\-[^\>]*$/s', $before, $matches)===1)	{
				$before = $matches[1];
			}
			if (is_array($subpartContent))	{
				$matches = array();
				if (preg_match('/^([^\<]*\-\-\>)(.*)(\<\!\-\-[^\>]*)$/s', $between, $matches)===1)	{
					$between = $matches[2];
				} elseif (preg_match('/^(.*)(\<\!\-\-[^\>]*)$/s', $between, $matches)===1)	{
					$between = $matches[1];
				} elseif (preg_match('/^([^\<]*\-\-\>)(.*)$/s', $between, $matches)===1)	{
					$between = $matches[2];
				}
			}
			$matches = array();
			if (preg_match('/^[^\<]*\-\-\>(.*)$/s', $after, $matches)===1)	{
				$after = $matches[1];
			}
		}

		if (is_array($subpartContent))	{
			$between = $subpartContent[0].$between.$subpartContent[1];
		} else	{
			$between = $subpartContent;
		}

		return $before.$between.$after;
	}
	function substituteMarker($marker,$result,$content)
	{
		return str_replace($marker,$result,$content);
	}
	/**
	 * Traverses the input $markContentArray array and for each key the marker by the same name (possibly wrapped and in upper case) will be substituted with the keys value in the array.
	 * This is very useful if you have a data-record to substitute in some content. In particular when you use the $wrap and $uppercase values to pre-process the markers. Eg. a key name like "myfield" could effectively be represented by the marker "###MYFIELD###" if the wrap value was "###|###" and the $uppercase boolean true.
	 *
	 * @param	string		The content stream, typically HTML template content.
	 * @param	array		The array of key/value pairs being marker/content values used in the substitution. For each element in this array the function will substitute a marker in the content stream with the content.
	 * @param	string		A wrap value - [part 1] | [part 2] - for the markers before substitution
	 * @param	boolean		If set, all marker string substitution is done with upper-case markers.
	 * @return	string		The processed output stream
	 * @see substituteMarker(), substituteMarkerInObject(), TEMPLATE()
	 */
	function substituteMarkerArray($content,$markContentArray,$wrap='',$uppercase=0)	{
		//var_dump(is_array($markContentArray));
		if (is_array($markContentArray) || is_object($markContentArray))	{
			reset($markContentArray);
			
		//	var_dump($markContentArray);
		foreach ($markContentArray as $marker=>$markContent){
		//	while(list($marker,$markContent)=each($markContentArray))	{
				$marker="###".$marker."###";
				$content=str_replace($marker,$markContent,$content);
			}

		}
		return $content;
	}
	function substituteAllMarker($result,$content)
	{
		return ereg_replace("###[[:alnum:]_]+###",$result,$content);
		
	}
	//link($detailPageLink,	$this->table[$subpart]->detailPage,	$this->table[$subpart]->uniqueUid,Array("uid"=>$fila['uid'])
 	function link2($path,$table,$conf=null)
 	{


 		$table=$TABLES[$table];
 		$conf["urlName"]=$table->urlName;
 		$path="/|urlName/|tagcategory:uid/|tagcategory:canonical-name/page/|page/";
 		var_dump($conf);
 	//	var_dump($_GET);
 		$pathPieces=explode("/",$path);
 		$pathPiecesReturn=Array();
 		foreach($pathPieces as $piece)
 		{
 			if(strchr($piece,"|")!==FALSE){
 			
 				$valor=sscanf($piece,"|%s");
 			
	 			if (isset($conf[$valor[0]]))
	 			{
	 				//var_dump($conf[$valor[0]]);
	 				 		$pathPiecesReturn[]=$conf[$valor[0]];
	 			}
	 			if (strchr($valor[0],":")!==FALSE)
	 			{
	 			
	 				list($nombrematriz, $nombrekey) = explode(":",$valor[0]);
		 			if (isset($_GET[$nombrematriz][$nombrekey]))
		 			{
		 				 	$pathPiecesReturn[]=$_GET[$nombrematriz][$nombrekey];		
		 			}
	 			}
 			}else{
 					$pathPiecesReturn[]=$piece; 				
 			}
 		}
 		
 		
 
 		return "<a href='".ROOT.implode("/",$pathPiecesReturn)."'>$value</a>";
	}
	
	function link($value,$dest,$table=null,$conf=null,$title=null)
	{	//valor puede ser un array 
		//var_dump($conf);
		/*$table[$subpart]*/
		//recipe[page]=3&recipe[person]=1&recipe[order]=title
		if($table===null)
		{
			if ($value==NULL)
				return Array("<a href='$dest' $title>","</a>");
			return("<a href='$dest' $title>$value</a>");
		}else{
			global $TABLES;
			$table=$TABLES[$table];
			$title=($title!==null)?"title='$title'":"";
			if (isset($conf))
			{
				if ($value==NULL)
				return Array("<a href='".ROOT.$table->urlName.URL::parseArray($conf,$table,"&amp;",$dest)."'>","</a>");
				return "<a href='".ROOT.$table->urlName.URL::parseArray($conf,$table,"&amp;",$dest)."'>$value</a>";
				/*else{
					$returnArray=Array();
					foreach($value as $item)					
						$returnArray[]="<a href='/recetas.es/$table->urlName".URL::parseArray($conf,$table,"&amp;")."'>$item</a>";
					return $returnArray;
					
				}*/
			}
		
		}
		
	
	}
}
?>